package com.aaa.forum.controller;


import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.config.AppConfig;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.Collection;
import com.aaa.forum.model.User;
import com.aaa.forum.services.ICollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "收藏夹接口")
@Slf4j
@RestController
@RequestMapping("/collection")
public class CollectionController {

    @Resource
    private ICollectionService collectionService;

    @ApiOperation("创建收藏夹")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("收藏夹名称") @RequestParam("name") String name) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        collectionService.create(user.getId(), name);
        return AppResult.success();
    }

    @ApiOperation("获取收藏夹列表")
    @PostMapping("/getList")
    public AppResult<List<Collection>> getList(HttpServletRequest request,
                                               @ApiParam("用户id") @RequestParam(value = "userId", required = false) Long userId) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        List<Collection> resultList;
        if (userId == null) {
            // 没有传递 userId 参数, 获取自己的收藏夹列表
            resultList = collectionService.selectALlByUserId(user.getId(), true);
        } else {
            // 获取他人的收藏夹列表
            resultList = collectionService.selectALlByUserId(userId, false);
        }
        return AppResult.success(resultList);
    }

    @ApiOperation("修改收藏夹名称")
    @PostMapping("/modifyName")
    public AppResult modifyName(HttpServletRequest request,
                                @ApiParam("收藏夹id") @RequestParam("id") Long id,
                                @ApiParam("收藏夹名称") @RequestParam("name") String name) {
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 根据id, 获取收藏夹信息
        Collection collection = collectionService.selectById(id);

        // 校验是否存在
        if (collection == null || collection.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_COLLECTION_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_COLLECTION_NOT_EXISTS));
        }

        // 校验收藏夹是否属于此用户
        if (user.getId() != collection.getUserId()) {
            log.warn(ResultCode.FAILED_FORBIDDEN.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_FORBIDDEN));
        }

        if (!name.equals(collection.getName())) {
            // 校验通过, 调用service
            collectionService.modifyName(id, name);
        }

        return AppResult.success();
    }

    @ApiOperation("修改隐私设置")
    @PostMapping("/modifyState")
    public AppResult modifyState(HttpServletRequest request,
                                 @ApiParam("收藏夹id") @RequestParam("id") Long id,
                                 @ApiParam("收藏夹隐私状态") @RequestParam("state") Byte state) {
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 根据id, 获取收藏夹信息
        Collection collection = collectionService.selectById(id);

        // 校验是否存在
        if (collection == null || collection.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_COLLECTION_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_COLLECTION_NOT_EXISTS));
        }

        // 校验收藏夹是否属于此用户
        if (user.getId() != collection.getUserId()) {
            log.warn(ResultCode.FAILED_FORBIDDEN.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_FORBIDDEN));
        }

        if (state != collection.getState()) {
            // 校验通过, 调用service
            collectionService.modifyState(id, state);
        }

        return AppResult.success();
    }

    @ApiOperation("删除收藏夹")
    @PostMapping("/deleteById")
    public AppResult deleteById(HttpServletRequest request,
                                @ApiParam("收藏夹id") @RequestParam("id") Long id) {
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 根据id, 获取收藏夹信息
        Collection collection = collectionService.selectById(id);

        // 校验是否存在
        if (collection == null || collection.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_COLLECTION_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_COLLECTION_NOT_EXISTS));
        }

        // 校验收藏夹是否属于此用户
        if (user.getId() != collection.getUserId()) {
            log.warn(ResultCode.FAILED_FORBIDDEN.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_FORBIDDEN));
        }

        // 校验通过, 调用service
        collectionService.deleteById(id);

        return AppResult.success();
    }
}
